//
//  FQDAllColumnsFeedCell.m
//  FakeQDaily
//
//  Created by mewxiv on 9/20/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDAllColumnsFeedCell.h"
#import "FQDSubscribeManager.h"
#import "FQDColumnFeedCellItem.h"
#import "FQDRestModel.h"

#import "UIImageView+WebCache.h"
#import "Masonry.h"

static NSString *const kSubscribeImageName = @"columnInfoSubscribe";
static NSString *const kUnSubscribeImageName = @"subscribed";

@interface FQDAllColumnsFeedCell ()

@property (nonatomic, strong) UIImageView *descImageView;
@property (nonatomic, strong) UILabel *columnNameLabel;
@property (nonatomic, strong) UILabel *descLabel;
@property (nonatomic, strong) UILabel *subscriptionLabel;
@property (nonatomic, strong) UIButton *subscribeButton;

@end

@implementation FQDAllColumnsFeedCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareUI
{
    _descImageView = [[UIImageView alloc] init];
    _descImageView.contentMode = UIViewContentModeScaleAspectFill;
    _descImageView.clipsToBounds = YES;
    [self.contentView addSubview:_descImageView];
    
    _columnNameLabel = [[UILabel alloc] init];
    _columnNameLabel.font = FQDBoldFontWithSize(15.0f);
    _columnNameLabel.textColor = RGB(42, 42, 42);
    [self.contentView addSubview:_columnNameLabel];
    
    _descLabel = [[UILabel alloc] init];
    _descLabel.font = FQDRegularFontWithSize(14.0f);
    _descLabel.textColor = RGB(51, 51, 51);
    _descLabel.numberOfLines = 2;
    _descLabel.preferredMaxLayoutWidth = 142.0f;
    [self.contentView addSubview:_descLabel];
    
    _subscriptionLabel = [[UILabel alloc] init];
    _subscriptionLabel.font = FQDRegularFontWithSize(12.0f);
    _subscriptionLabel.textColor = RGB(115, 115, 115);
    _subscriptionLabel.numberOfLines = 2;
    _subscriptionLabel.preferredMaxLayoutWidth = 142.0f;
    [self.contentView addSubview:_subscriptionLabel];
    
    UIImage *subscribeImage = [UIImage imageNamed:kSubscribeImageName];
    _subscribeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_subscribeButton setImage:subscribeImage forState:UIControlStateNormal];
    [self.contentView addSubview:_subscribeButton];
    
    [self.subscribeButton addTarget:self action:@selector(subscribeColumn:)
                   forControlEvents:UIControlEventTouchUpInside];
}

- (void)prepareConstraints
{
    WeakSelf;
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf);
    }];
    
    [_descImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView.mas_left).offset(12);
        make.top.equalTo(weakSelf.contentView.mas_top).offset(12);
        make.bottom.equalTo(weakSelf.contentView).offset(-12);
        make.size.equalTo(@91);
    }];
    
    [_columnNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.descImageView).offset(1);
        make.left.equalTo(weakSelf.descImageView.mas_right).offset(8.5);
        make.right.equalTo(weakSelf.contentView).offset(-13);
    }];
    
    [_descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.columnNameLabel.mas_bottom).offset(5);
        make.left.equalTo(weakSelf.columnNameLabel);
    }];
    
    [_subscriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.columnNameLabel);
        make.right.equalTo(weakSelf.descLabel);
        make.bottom.equalTo(weakSelf.descImageView);
    }];
    
    [_subscribeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.contentView).offset(-12);
        make.centerY.equalTo(weakSelf.contentView);
        make.size.equalTo(@30);
    }];
}

- (void)subscribeColumn:(UIButton *)subscribeButton
{
    if ([self.delegate respondsToSelector:@selector(collectionViewCell:actionType:actionControl:userInfo:)]) {
        [self.delegate collectionViewCell:self actionType:FQDAllColumnsFeedCellActionSubscribe
                            actionControl:subscribeButton userInfo:nil];
    }
}

- (void)configureCellWithCellItem:(FQDColumnFeedCellItem *)cellItem
{
    [super configureCellWithCellItem:cellItem];
    
    FQDColumn *column = cellItem.column;
    
    NSURL *imageURL = [NSURL URLWithString:column.image];
    [self.descImageView sd_setImageWithURL:imageURL];
    
    self.columnNameLabel.text = column.name;
    self.descLabel.text = column.desc;
    
    NSString *subscribeString = [NSString stringWithFormat:@"%@人已订阅，更新至%@篇",
                              @(column.subscriber_num), @(column.post_count)];
    self.subscriptionLabel.text = subscribeString;
    
    FQDSubscribeManager *subscribeManager = [FQDSubscribeManager sharedManager];
    BOOL hasSubscribed = [subscribeManager hasSubscribedColumn:column.ID];
    self.subscribed = hasSubscribed;
}

- (void)setSubscribed:(BOOL)subscribed
{
    _subscribed = subscribed;
    
    NSString *subscribeImageName = subscribed ? kUnSubscribeImageName : kSubscribeImageName;
    [self.subscribeButton setImage:[UIImage imageNamed:subscribeImageName] forState:UIControlStateNormal];
}

+ (NSString *)reuseId
{
    return @"FQDAllColumnsFeedCell";
}

@end
